#include "atomic_0813_status_tracking_service.h"

// 实现状态事件推送接口
int pushStatusEvent(StatusEventInput *input, StatusEventOutput *output) {
    // 检查输入输出参数有效性
    if (input == nullptr || output == nullptr) {
        return ATOMIC_SERVICE_ERROR;
    }

    // 初始化输出参数默认值
    output->return_type = ATOMIC_SERVICE_SUCCESS;
    output->rule_id = 0;
    output->rule_name = "";
    output->condition = "";
    output->action = "";
    output->log_level = 0;
    output->content = "";
    output->module_name = "atomic_0813_status_tracking_service";

    // 验证输入事件的基本字段有效性
    if (input->service_id <= 0 || input->task_id <= 0 || input->status.empty() || input->timestamp <= 0) {
        output->return_type = ATOMIC_SERVICE_FAIL;
        output->log_level = 3; // ERROR级别
        output->content = "Invalid input parameters: service_id, task_id, status or timestamp is invalid";
        return output->return_type;
    }

    // 根据不同状态设置规则和动作（示例规则逻辑）
    if (input->status == "Running") {
        output->rule_id = 1001;
        output->rule_name = "RunningStatusRule";
        output->condition = "status == \"Running\"";
        output->action = "notify_monitor_dashboard";
        output->log_level = 1; // INFO级别
        output->content = "Service [" + input->service_name + "(" + std::to_string(input->service_id) + 
                         ")] task " + std::to_string(input->task_id) + " started running at " + 
                         std::to_string(input->timestamp);
    } 
    else if (input->status == "Failed") {
        output->rule_id = 1002;
        output->rule_name = "FailedStatusRule";
        output->condition = "status == \"Failed\"";
        output->action = "send_alert_email;create_ticket";
        output->log_level = 3; // ERROR级别
        output->content = "Service [" + input->service_name + "(" + std::to_string(input->service_id) + 
                         ")] task " + std::to_string(input->task_id) + " failed at " + 
                         std::to_string(input->timestamp);
    } 
    else if (input->status == "Completed") {
        output->rule_id = 1003;
        output->rule_name = "CompletedStatusRule";
        output->condition = "status == \"Completed\"";
        output->action = "archive_task;update_metrics";
        output->log_level = 1; // INFO级别
        output->content = "Service [" + input->service_name + "(" + std::to_string(input->service_id) + 
                         ")] task " + std::to_string(input->task_id) + " completed successfully at " + 
                         std::to_string(input->timestamp);
    } 
    else {
        output->rule_id = 9999;
        output->rule_name = "UnknownStatusRule";
        output->condition = "status is unknown";
        output->action = "log_unknown_status";
        output->log_level = 2; // WARN级别
        output->content = "Service [" + input->service_name + "(" + std::to_string(input->service_id) + 
                         ")] task " + std::to_string(input->task_id) + " has unknown status: " + 
                         input->status + " at " + std::to_string(input->timestamp);
    }

    return output->return_type;
}